package com.rogue.blog.controller.article;

import com.rogue.blog.model.dto.article.ArticleCategoryDTO;
import com.rogue.blog.model.po.PageBean;
import com.rogue.blog.model.po.PageCondition;
import com.rogue.blog.model.vo.ResponseVO;
import com.rogue.blog.model.vo.article.ArticleCategoryVO;
import com.rogue.blog.service.article.ArticleCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by 86101 on 2018-10-09.
 */
@Api(tags = {"文章分类"})
@RestController
@RequestMapping(value = "")
public class ArticleCategoryController {

    private final static Logger logger = LoggerFactory.getLogger(ArticleCategoryController.class);

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @ApiOperation(value = "添加文章分类")
    @RequestMapping(value = "/articleCategory", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Long> insert(@RequestBody ArticleCategoryDTO dto) throws Exception {
        long categoryId = articleCategoryService.insert(dto);
        ResponseVO<Long> responseVO = new ResponseVO<>();
        if (StringUtils.isEmpty(categoryId)) {
            responseVO.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseVO.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.name());
            responseVO.setMessage("Insert article category error.");
        } else {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Insert article category success.");
            responseVO.setData(categoryId);
        }
        return responseVO;
    }

    @ApiOperation(value = "根据ID删除文章分类")
    @RequestMapping(value = "/articleCategory/{categoryId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Integer> deleteById(@PathVariable long categoryId) throws Exception {
        ResponseVO<Integer> responseVO = new ResponseVO<>();
        int row = articleCategoryService.deleteById(categoryId, false);
        if (0 == row) {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
        } else {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
        }
        responseVO.setMessage("Affected rows " + row + ".");
        responseVO.setData(row);
        return responseVO;
    }

    @ApiOperation(value = "根据ID更新文章分类")
    @RequestMapping(value = "/articleCategory/{categoryId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Integer> updateById(@RequestBody ArticleCategoryDTO dto, @PathVariable long categoryId) throws Exception {
        ResponseVO<Integer> responseVO = new ResponseVO<>();
        ArticleCategoryVO articleCategoryVO = articleCategoryService.selectById(categoryId);
        if (null == articleCategoryVO) {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
            responseVO.setMessage("Not found article category by the id " + categoryId + ". Please check the parameters.");
        } else {
            int row = articleCategoryService.updateById(categoryId, dto);
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Affected rows " + row + ".");
            responseVO.setData(row);
        }
        return responseVO;
    }

    @ApiOperation(value = "根据ID获取文章分类")
    @RequestMapping(value = "/articleCategory/{categoryId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<ArticleCategoryVO> selectById(@PathVariable long categoryId) throws Exception {
        ResponseVO<ArticleCategoryVO> responseVO = new ResponseVO<>();
        ArticleCategoryVO articleCategoryVO = articleCategoryService.selectById(categoryId);
        if (null == articleCategoryVO) {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
            responseVO.setMessage("Not found article category by the id " + categoryId + ". Please check the parameters.");
        } else {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Found article category by the id " + categoryId + ".");
            responseVO.setData(articleCategoryVO);
        }
        return responseVO;
    }

    @ApiOperation(value = "分页获取文章分类")
    @RequestMapping(value = "/articleCategorys", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<PageBean<ArticleCategoryVO>> selectByPage(@RequestBody PageCondition pageCondition) throws Exception {
        ResponseVO<PageBean<ArticleCategoryVO>> responseVO = new ResponseVO<>();
        PageBean<ArticleCategoryVO> dtoPageBean = articleCategoryService.selectByPage(pageCondition);
        PageBean<ArticleCategoryVO> voPageBean = dtoPageBean.toPageBean(ArticleCategoryVO.class);
        if (CollectionUtils.isEmpty(dtoPageBean.getPageData())) {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
            responseVO.setMessage("Not found article categorys. Please check the parameters.");
            responseVO.setData(voPageBean);
        } else {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Found article categorys.");
            responseVO.setData(voPageBean);
        }
        return responseVO;
    }

    @ApiOperation(value = "文章分类是否存在")
    @RequestMapping(value = "/articleCategory/exists", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Boolean> exists(@RequestParam String categoryName) throws Exception {
        ResponseVO<Boolean> responseVO = new ResponseVO<>();
        boolean exists = articleCategoryService.exists(categoryName);
        if (exists) {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Found article category by the categoryName " + categoryName + ".");
            responseVO.setData(exists);
        } else {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
            responseVO.setMessage("Not found article category by the categoryName " + categoryName + ".");
            responseVO.setData(exists);
        }
        return responseVO;
    }
}
